package com.kelvin.oocl.processer.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.kelvin.oocl.constart.Expression;
import com.kelvin.oocl.pojo.Customer;
import com.kelvin.oocl.pojo.CustomerVo;
import com.kelvin.oocl.processer.DataProcesser;
import com.kelvin.oocl.util.DateUtil;

public class DataProcessorImpl implements DataProcesser{

	@Override
	public List<Customer> process(List<CustomerVo> data) {
		if(data == null || data.size()==0){
			return null;
		}
		try {
			List<Customer> result = new ArrayList<>();
			for (CustomerVo customer : data) {
				if(isGoodCustomer(customer)){
					result.add(toCustomer(customer));
				}
			}
			return result;
		} catch (ParseException e) {
			return null;
		}
	}
	
	private Customer toCustomer(CustomerVo customer) throws ParseException {
		Customer c = new Customer();
		c.setId(customer.getId());
		c.setName(customer.getName());
		c.setEmail(customer.getEmail());
		c.setSex(Boolean.parseBoolean(customer.getSex()));
		c.setBirthDate(DateUtil.strToDate(customer.getBirthDate(), "yyyy-MM-dd"));
		return c;
	}

	private boolean isGoodCustomer(CustomerVo customer){
		boolean flag = (isGoodId(customer.getId()) && isGoodName(customer.getName()) && 
				isGoodMail(customer.getEmail()) && isGoodSex(customer.getSex()) && 
				isGoodBirthDate(customer.getBirthDate()));
		return flag;
	}
	
	private boolean isGoodId(String id){
		return id.matches(Expression.ID_FILTER);
	}
	
	private boolean isGoodName(String Name){
		return Name.matches(Expression.NAME_FILTER);
	}
	
	private boolean isGoodMail(String email){
		return email.matches(Expression.EMAIL_FILTER);
	}
	
	private boolean isGoodSex(String sex){
		return sex.matches(Expression.SEX_FILTER);
	}
	
	private boolean isGoodBirthDate(String birthDate){
		return birthDate.matches(Expression.BIRTHDATE_FILTER);
	}
	
}
